package pwsaver;

import com.google.appengine.api.datastore.*;
import com.google.appengine.api.users.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import java.util.logging.Logger;

public class PinInfoDAO {
	
	private static final Logger log = Logger.getLogger(PinDaoServlet.class.getName());

	private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

	public UserInfo createUserInfo(User user) {
		UserInfo userInfo = UserInfo.CreateUserInfo(user);
		Entity userInfoEntity = new Entity(UserInfo.class.getSimpleName(),
				userInfo.getUserKey());
		userInfoEntity.setProperty("user", user);
		userInfoEntity.setProperty("createDate", userInfo.getCreateDate());
		this.datastore.put(userInfoEntity);
		return userInfo;
	}
	
	public UserInfo getOrCreateUserInfo(User user){
		try {
			Entity userInfoEntity = this.datastore.get(UserInfo.generateUserKey(user));
			return new UserInfo((User)userInfoEntity.getProperty("user"),(Date)userInfoEntity.getProperty("createDate"));
		} catch (EntityNotFoundException e) {
			log.info(String.format("user {0} doesn't exist, will be created.", user.getNickname()));
			return createUserInfo(user);
		}
	}
	
	public void deleteUserInfo(User user){
		this.datastore.delete(UserInfo.generateUserKey(user));
	}
	
	public void UpdatePinInfo(UserInfo userInfo, List<PinInfo> pinInfos){
		
		List<Entity> pinInfoEntities = new ArrayList<Entity>();
		for(PinInfo pinInfo:pinInfos){
			Entity pinInfoEntity = new Entity(PinInfo.class.getSimpleName(), pinInfo.getSource(), userInfo.getUserKey());
			
			pinInfoEntity.setProperty("account", pinInfo.getAccount());
			pinInfoEntity.setProperty("pin", pinInfo.getPin());
			pinInfoEntity.setProperty("type", pinInfo.getType());
			pinInfoEntity.setProperty("comment", pinInfo.getComment());
			pinInfoEntity.setProperty("modifyDate", pinInfo.getModifyDate());
			
			pinInfoEntities.add(pinInfoEntity);
					
		}
		
		this.datastore.put(pinInfoEntities);
	}
	
	public List<PinInfo> getPinInfoList(UserInfo userInfo){
		Query pinInfoQuery = new Query(PinInfo.class.getSimpleName());
		pinInfoQuery.setAncestor(userInfo.getUserKey());

		// This returns weddingPhoto, babyPhoto and dancePhoto, but
		// not campingPhoto because tom is not an ancestor.
		List<Entity> entities = datastore.prepare(pinInfoQuery).asList(
		    FetchOptions.Builder.withDefaults());
		List<PinInfo> pinInfos = new ArrayList<PinInfo>();
		for(Entity entity:entities){
			PinInfo pinInfo = new PinInfo();
			pinInfo.setSource(entity.getKey().getName());
			pinInfo.setAccount(entity.getProperty("account").toString());
			pinInfo.setPin(entity.getProperty("pin").toString());
			pinInfo.setType(entity.getProperty("type").toString());
			pinInfo.setComment(entity.getProperty("comment").toString());
			pinInfo.setModifyDate((Date)entity.getProperty("modifyDate"));
			pinInfos.add(pinInfo);
		}
		
		return pinInfos;
		
	}

	/***
	 * public void insert(Customer customer) throws CustomerDAOException;
	 * 
	 * public void update(CustomerPK pk, Customer customer) throws
	 * CustomerDAOException;
	 * 
	 * public void delete(CustomerPK pk) throws CustomerDAOException;
	 * 
	 * public Customer[] findAll() throws CustomerDAOException;
	 * 
	 * public Customer findByPrimaryKey(String email) throws
	 * CustomerDAOException;
	 * 
	 * public Customer[] findByCompany(int companyId) throws
	 * CustomerDAOException;
	 ***/
}
